package com.example.administrator.u17.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.ActionBarOverlayLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.u17.activity.WebViewActivity;
import com.example.administrator.u17.adapter.TuiJianAdapter;
import com.example.administrator.u17.adapter.TuijianViewPageAdapter;
import com.example.administrator.u17.config.NetConfig;
import com.example.administrator.u17.model.TuiJainRootBean;
import com.example.administrator.u17.model.TuiJianComicLists;
import com.example.administrator.u17.R;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.picasso.Picasso;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/14.
 */

public class TuiJianFragment extends Fragment{
    private PullToRefreshListView listView;
    private List<TuiJianComicLists> list;
    private BaseAdapter adapter;
    private ImageView imageView;
    private ListView lv;
    private ImageView iv1,iv2,iv3,iv4,iv5;
    private TuiJainRootBean bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tuijian,null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",NetConfig.VIEW_PAGER_ONE);
                startActivity(intent);
            }
        });
        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",NetConfig.VIEW_PAGER_TWO);
                startActivity(intent);
            }
        });
    }

    private void setData() {
        loadData();
    }

    private void initData() {
        list = new ArrayList<>();
        adapter = new TuiJianAdapter(getActivity(), list);
        lv = new ListView(getActivity());
        lv = listView.getRefreshableView();

        View v = View.inflate(getActivity(), R.layout.view_viewpager, null);
        ViewPager vp = (ViewPager) v.findViewById(R.id.vp_view_viewPager);
        List<View> listV = new ArrayList<>();

        View ivView = View.inflate(getActivity(), R.layout.view_imagview, null);
        iv1 = (ImageView) ivView.findViewById(R.id.iv_view_ImageView);
        iv1.setImageResource(R.mipmap.comic_comment_empty);

        View ivView1 = View.inflate(getActivity(), R.layout.view_imagview, null);
        iv2 = (ImageView) ivView1.findViewById(R.id.iv_view_ImageView);
        iv2.setImageResource(R.mipmap.comic_comment_empty);

        View ivView2 = View.inflate(getActivity(), R.layout.view_imagview, null);
        iv3 = (ImageView) ivView2.findViewById(R.id.iv_view_ImageView);
        iv3.setImageResource(R.mipmap.comic_comment_empty);

        View ivView3 = View.inflate(getActivity(), R.layout.view_imagview, null);
        iv4 = (ImageView) ivView3.findViewById(R.id.iv_view_ImageView);
        iv4.setImageResource(R.mipmap.comic_comment_empty);

        View ivView4 = View.inflate(getActivity(), R.layout.view_imagview, null);
        iv5 = (ImageView) ivView4.findViewById(R.id.iv_view_ImageView);
        iv5.setImageResource(R.mipmap.comic_comment_empty);
        listV.add(ivView);
        listV.add(ivView1);
        listV.add(ivView2);
        listV.add(ivView3);
        listV.add(ivView4);

        TuijianViewPageAdapter tuijianViewPageAdapter = new TuijianViewPageAdapter(listV);
        vp.setAdapter(tuijianViewPageAdapter);
        lv.addHeaderView(v);

        lv.setAdapter(adapter);
    }

    private void initView(View view) {
        listView = (PullToRefreshListView) view.findViewById(R.id.listview_fragment_tuijian);
    }

    private void loadData() {
        RequestParams params = new RequestParams(NetConfig.TUIJIAN_PATH);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                Gson g = new Gson();
                bean = g.fromJson(s, TuiJainRootBean.class);
                list.addAll(bean.data.returnData.comicLists);
                Picasso.with(getActivity()).load(bean.data.returnData.galleryItems.get(0).cover).into(iv1);
                Picasso.with(getActivity()).load(bean.data.returnData.galleryItems.get(1).cover).into(iv2);
                Picasso.with(getActivity()).load(bean.data.returnData.galleryItems.get(2).cover).into(iv3);
                Picasso.with(getActivity()).load(bean.data.returnData.galleryItems.get(3).cover).into(iv4);
                Picasso.with(getActivity()).load(bean.data.returnData.galleryItems.get(4).cover).into(iv5);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                Toast.makeText(getActivity(), "网络异常", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
